package com.shunwang.tools.httpclientsupport.pool;

import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool.Config;

import com.shunwang.tools.httpclientsupport.client.AbstractHttpClientNode;
import com.shunwang.tools.httpclientsupport.client.HttpClientInfo;
import com.shunwang.tools.httpclientsupport.client.HttpClientNode;

public class HttpClientPool extends Pool<AbstractHttpClientNode> {

	
	
	public HttpClientPool(Config poolConfig, PoolableObjectFactory factory) {
		super(poolConfig, factory);
	}
	
	public HttpClientPool(Config poolConfig,HttpClientInfo info){
		this(poolConfig,new HttpClientFactory(info));
	}

	private static class HttpClientFactory extends BasePoolableObjectFactory{

		private HttpClientInfo clientInfo;
		
		public HttpClientFactory(HttpClientInfo info){
			this.clientInfo = info;
		}
		@Override
		public Object makeObject() throws Exception {
			return new HttpClientNode(clientInfo);
		}
		@Override
		public void destroyObject(Object obj) throws Exception {
			if(obj instanceof HttpClientNode){
				((HttpClientNode)obj).disconnect();
			}
		}
		
	}
}
